package com.sctvcloud.qingchuan.ui.adapter;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.ViewGroup;

import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.beans.FocusColumn;
import com.sctvcloud.qingchuan.ui.fragment.AnchorFragment;
import com.sctvcloud.qingchuan.ui.fragment.AttentionFragment;
import com.sctvcloud.qingchuan.ui.fragment.CityStateFragment;
import com.sctvcloud.qingchuan.ui.fragment.ColumnBaseFragment;
import com.sctvcloud.qingchuan.ui.fragment.CommonFragment;
import com.sctvcloud.qingchuan.ui.fragment.CountryFragment;
import com.sctvcloud.qingchuan.ui.fragment.CultureTravelFragment;
import com.sctvcloud.qingchuan.ui.fragment.FinancialFragment;
import com.sctvcloud.qingchuan.ui.fragment.GovStuffFragment;
import com.sctvcloud.qingchuan.ui.fragment.GreatestFragment;
import com.sctvcloud.qingchuan.ui.fragment.HighRiseFragment;
import com.sctvcloud.qingchuan.ui.fragment.LiveFragment;
import com.sctvcloud.qingchuan.ui.fragment.NewsListFragment;
import com.sctvcloud.qingchuan.ui.fragment.SpecialFragment;
import com.sctvcloud.qingchuan.ui.fragment.TvFragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/19 0019.
 * 首页 管理各个栏目的页面适配器
 */

public class HomePagerAdapter extends FragmentStatePagerAdapter {

    public static final int TYPE_HIGH_RISE = 0;//高层
    public static final int TYPE_SIFT = 1;//精选
    public static final int TYPE_NEWS = 2;//新闻
    public static final int TYPE_BTV = 3;//卫视
    public static final int TYPE_LIVE = 4;//直播
    public static final int TYPE_COMMON = 7;//要闻
    public static final int TYPE_ACT = 11;//活动
    public static final int TYPE_SPECIAL = 12;//专题
    public static final int TYPE_CITY = 13;//政务
    public static final int TYPE_COUNTRY = 14;//乡镇
    public static final int TYPE_CULTURE = 15;//文旅
    public static final int TYPE_ATTENTION = 25;//关注(乡镇部门)

    private List<FocusColumn> list;
    private TvFragment tv;
//    private FocusColumn specail;
//    private FocusColumn anchor;
    private SparseArray<WeakReference<ColumnBaseFragment>> fragmentWeaks;

    private int topHeight;

    public HomePagerAdapter(FragmentManager fm, List<FocusColumn> list) {
        super(fm);
        this.list = list;
        fragmentWeaks = new SparseArray<>();
    }
    /**
     * 过滤未在设计范围内的栏目
     *
     * @param data 后台来源栏目数据
     * @return 过滤后的栏目数据
     */
    public static List<FocusColumn> handleColumn(List<FocusColumn> data) {
        List<FocusColumn> result = new ArrayList<>();
        if (ListUtils.isListValued(data)) {
            for (int i = 0; i < data.size(); i++) {
                switch (data.get(i).getChannelType()) {
                    case "" + TYPE_HIGH_RISE:
                    case "" + TYPE_SIFT:
                    case "" + TYPE_NEWS:
                    case "" + TYPE_BTV:
                    case "" + TYPE_LIVE:
                    case "" + TYPE_CULTURE:
                    case "" + TYPE_SPECIAL:
                    case "" + TYPE_CITY:
                    case "" + TYPE_COUNTRY:
                    case "" + TYPE_COMMON:
                    case "" + TYPE_ACT:
                    case "" + TYPE_ATTENTION:
                        result.add(data.get(i));
                        break;
                    default:
                        break;
                }
            }
        }
        return result;
    }
    @Override
    public Fragment getItem(int position) {
        ColumnBaseFragment fragment = null;
        switch (list.get(position).getChannelType()) {
            case "" + TYPE_HIGH_RISE:
                fragment = new HighRiseFragment();
                break;
            case "" + TYPE_SIFT:
                fragment = new GreatestFragment();
//                ((GreatestFragment) fragment).setAnchorChannelUrl();
//                if(null!=specail) {
//                    ((GreatestFragment) fragment).setSpecialUrlTemp(specail.getChannelUrl());
//                }
//                if(null!=anchor) {
//                    ((GreatestFragment) fragment).setAnchorsUrlTemp(anchor.getChannelUrl());
//                }
                break;
            case "" + TYPE_NEWS:
                fragment = new NewsListFragment();
                break;
            case "" + TYPE_BTV:
                fragment = new TvFragment();
                break;
            case "" + TYPE_LIVE:
                fragment = new LiveFragment();
                break;
            case "" + TYPE_CULTURE:
                fragment = new CultureTravelFragment();
                break;
//            case "" + TYPE_ANCHOR:
//                fragment = new AnchorFragment().setOtherHeight(topHeight);
//
//                break;
            case "" + TYPE_SPECIAL:
                fragment = new SpecialFragment();
                fragment.setTag(list.get(position).getChannelName());
                break;
            case "" + TYPE_CITY:
                Bundle city = new Bundle();
                city.putString("channelId", String.valueOf(list.get(position).getChannelId()));
                fragment = new GovStuffFragment();
                fragment.setArguments(city);
                break;
            case "" + TYPE_COUNTRY:
                Bundle country = new Bundle();
                country.putString("channelId", String.valueOf(list.get(position).getChannelId()));
                fragment = new CountryFragment();
                fragment.setTag(list.get(position).getChannelName());
                fragment.setArguments(country);

                break;
            case "" + TYPE_COMMON:
                fragment = new CommonFragment();
                fragment.setTag(list.get(position).getChannelName());
                break;
            case "" + TYPE_ACT:
                fragment = new CommonFragment();
                fragment.setAct(true);
                fragment.setTag(list.get(position).getChannelName());
                break;
            case "" + TYPE_ATTENTION:
                fragment = new FinancialFragment();
                break;
            default:
                fragment = new CommonFragment();
                break;
        }
        fragment.setChannelUrl(list.get(position).getChannelUrl());
        fragment.setIndexInViewPager(position);
        fragmentWeaks.put(position, new WeakReference<ColumnBaseFragment>(fragment));
        return fragment;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list.get(position).getChannelName();
    }

    public void setData(List<FocusColumn> data) {
        this.list = data;
//        for (int n = 0; n < list.size(); n++) {
//            if (list.get(n).getChannelType().equalsIgnoreCase("" + TYPE_SPECIAL)) {
//                specail = list.get(n);
//            } else if (list.get(n).getChannelType().equalsIgnoreCase("" + TYPE_ANCHOR)) {
//                anchor = list.get(n);
//                list.remove(n);
//            }
//        }
        notifyDataSetChanged();
    }

    public ColumnBaseFragment getFragmentByPos(int pos) {
        WeakReference<ColumnBaseFragment> wrf = fragmentWeaks.get(pos);
        if (wrf != null) {
            return wrf.get();
        }
        return null;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //在tab切换时，如果被切走的页面是“频道”页面，则不销毁。保证频道页面中的tab+Fragment布局不出异常
        if (null != list && list.size() > position && null != list.get(position) &&
                !list.get(position).getChannelType().equalsIgnoreCase("" + TYPE_BTV)) {
            //如果注释这行，那么不管怎么切换，page都不会被销毁
            super.destroyItem(container, position, object);
        }
    }

    public int getPosCurrentPage(String type) {
        if (getCount() == 0) {
            return -1;
        }
        for (int i = 0; i < getCount(); i++) {
            if (list.get(i).getChannelType().equals(type)) {
                return i;
            }
        }
        return -1;
    }

    public void setTopHeight(int topHeight) {
        this.topHeight = topHeight;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }


}
